package gw.com.android.ui.chart.views;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppContances;
import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import gw.com.android.ui.dialog.LoginDialog;
import gw.com.android.ui.dialog.PopupConfirmDialog;
import gw.com.android.ui.trade.TradeBusiness;
import gw.com.android.utils.ColorThemeUtil;
import gw.com.android.utils.MobclickEventUtlis;
import gw.com.jni.library.terminal.GTSConst;
import www.com.library.app.Logger;
import www.com.library.model.DataItemDetail;
import www.com.library.util.CommonUtils;
import www.com.library.util.NetworkMonitor;
import www.com.library.view.PriceTextView;

/**
 * 图片界面底部报价自定义控件
 *
 * @author reeta
 */
public class ChartPriceBtn extends RelativeLayout {

    @BindView(R.id.chart_price_btn_container)
    View container;
    /**
     * 卖出布局
     */
    @BindView(R.id.sell_price_layout)
    RelativeLayout mSaleLayout;
    /**
     * 买入布局
     */
    @BindView(R.id.buy_price_layout)
    RelativeLayout mBuyLayout;
    /**
     * 卖出title
     */
    @BindView(R.id.sell_price_title)
    TextView mSaleTitle;
    /**
     * 买入title
     */
    @BindView(R.id.buy_price_title)
    TextView mBuyTitle;
    /**
     * 卖出
     */
    @BindView(R.id.sell_price)
    PriceTextView mSaleV;
    /**
     * 买入
     */
    @BindView(R.id.buy_price)
    PriceTextView mBuyV;
    /**
     * 差价
     */
    @BindView(R.id.spreed_view)
    TextView mSpreedPrice;

    private DataItemDetail mTickModel;
    private boolean isNight = false;

    public ChartPriceBtn(Context context) {
        super(context);
        initView(context);
    }

    public ChartPriceBtn(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    public ChartPriceBtn(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public void initView(Context act) {
        View mView = LayoutInflater.from(act).inflate(R.layout.chart_price_btn, this, true);
        ButterKnife.bind(this, mView);
        mSaleTitle.setAlpha(0.6f);
        mBuyTitle.setAlpha(0.6f);
    }

    /**
     * 设置界面数据
     *
     * @param model
     */
    public void setTickData(DataItemDetail model) {
        if (model == null) {
            return;
        }

        mTickModel = model;

        // 卖价
        mSaleV.updateWithPrice(mTickModel.getString(GTSConst.JSON_KEY_SELLPRICE), mTickModel.getString(GTSConst.JSON_KEY_ZOOMRULE), false);
        mSaleTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0, getArrawId(mSaleLayout, mTickModel.getInt(GTSConst.JSON_KEY_SELLPRICESTATE), mTickModel.getInt(GTSConst.JSON_KEY_SELLPRICEARRAW)), 0);

        // 买价
        mBuyV.updateWithPrice(mTickModel.getString(GTSConst.JSON_KEY_BUYPRICE), mTickModel.getString(GTSConst.JSON_KEY_ZOOMRULE), false);
        mBuyTitle.setCompoundDrawablesWithIntrinsicBounds(getArrawId(mBuyLayout, mTickModel.getInt(GTSConst.JSON_KEY_BUYPRICESTATE), mTickModel.getInt(GTSConst.JSON_KEY_BUYPRICEARRAW)), 0, 0, 0);

        //点差
        mSpreedPrice.setText(mTickModel.getString(GTSConst.JSON_KEY_DIFF));
    }

    /**
     * 根据涨跌方向返回箭头
     *
     * @param direction
     * @param arrawState 1标识不需要箭头，0标识要显示箭头
     * @return
     */
    private int getArrawId(View view, int direction, int arrawState) {
        int arrawId = R.mipmap.a_price_standand;
        int bgId = R.drawable.price_unchange_bg;
        switch (direction) {
            case AppContances.PRICE_INCREASE:
                if (arrawState == AppContances.ARRAW_HIDE) {
                    arrawId = R.mipmap.a_price_standand;
                } else {
                    arrawId = R.mipmap.a_price_white_up;
                }
                if (ColorThemeUtil.instance().isGreenRise) {
                    bgId = getPriceRiseBg();
                } else {
                    bgId = getPriceDropBg();
                }
                break;
            case AppContances.PRICE_DECREASE:
                if (arrawState == AppContances.ARRAW_HIDE) {
                    arrawId = R.mipmap.a_price_standand;
                } else {
                    arrawId = R.mipmap.a_price_white_down;
                }
                if (ColorThemeUtil.instance().isGreenRise) {
                    bgId = getPriceDropBg();
                } else {
                    bgId = getPriceRiseBg();
                }
                break;
            case AppContances.PRICE_UNCHANGE:
                arrawId = R.mipmap.a_price_standand;
                bgId = R.drawable.price_unchange_bg;
                break;
        }
        view.setBackgroundResource(bgId);
        view.setTag(bgId);
        return arrawId;
    }

    private int getPriceDropBg() {
        return isNight ? R.drawable.price_drop_bg_night : R.drawable.price_drop_bg;
    }

    private int getPriceRiseBg() {
        return isNight ? R.drawable.price_rise_bg_night : R.drawable.price_rise_bg;
    }

    @OnClick(R.id.sell_price_layout)
    void clickSaleBtn() {
        if (CommonUtils.isFastDoubleClick()) {
            return;
        }
        if (!checkTradable()) {
            return;
        }
        MobclickEventUtlis.MobclickEventByAccountType(getContext(), "Chart_sell_click");
        ActivityManager.showOrderActivity((FragmentActivity) getContext(), mTickModel, AppContances.TRADE_TYPE_SALE, AppContances.TRADE_TYPE_FORM_CHART);
    }

    @OnClick(R.id.buy_price_layout)
    void clickBuyBtn() {
        if (CommonUtils.isFastDoubleClick()) {
            return;
        }
        if (!checkTradable()) {
            return;
        }
        MobclickEventUtlis.MobclickEventByAccountType(getContext(), "Chart_buy_click");
        ActivityManager.showOrderActivity((FragmentActivity) getContext(), mTickModel, AppContances.TRADE_TYPE_BUY, AppContances.TRADE_TYPE_FORM_CHART);
    }

    /**
     * 下单判断
     *
     * @return
     */
    private boolean checkTradable() {
        if (!NetworkMonitor.hasNetWork()) {
            return false;
        }

        if (null == mTickModel) {
            Logger.i("行情数据对象为空！！！");
            showErrorMsg(ConfigType.SERVER_ERROR_1021);
            return false;
        }

        // 游客登录时弹出提示
        if (GTConfig.instance().getAccountType() == GTConfig.ACC_TYPE_GUEST) {
            LoginDialog.showLoginDialog((FragmentActivity) getContext(), ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1013));
            return false;
        }

        if (!TradeBusiness.PreOrderCheck((FragmentActivity) getContext(), mTickModel.getInt(GTSConst.JSON_KEY_ZONE), mTickModel.getInt(GTSConst.JSON_KEY_TRADESTATE), true)) {
            return false;
        }

        return true;
    }

    private void showErrorMsg(String errorId) {
        final PopupConfirmDialog dialog = PopupConfirmDialog.newInstance((FragmentActivity) getContext(), "", ConfigUtil.instance().getErrorConfigObject().optString(errorId));
        dialog.show();
    }

    public void onNightMode(boolean isNight) {
        this.isNight = isNight;
        if (isNight) {
            mSpreedPrice.setBackgroundResource(R.drawable.price_spreed_bg_night);
            container.setBackgroundColor(getResources().getColor(R.color.color_night_bg_dark));
        } else {
            mSpreedPrice.setBackgroundResource(R.drawable.price_spreed_bg);
            container.setBackgroundColor(getResources().getColor(R.color.color_a));
        }
        changeNightMode(mBuyLayout,isNight);
        changeNightMode(mSaleLayout,isNight);
    }

    private void changeNightMode(View view, boolean isNight) {
        Object tag = view.getTag();
        if (tag instanceof Integer) {
            int resid = (int) tag;
            if (isNight && resid == R.drawable.price_drop_bg) {
                resid = R.drawable.price_drop_bg_night;
                view.setBackgroundResource(resid);
                view.setTag(resid);
            } else if (isNight && resid == R.drawable.price_rise_bg) {
                resid = R.drawable.price_rise_bg_night;
                view.setBackgroundResource(resid);
                view.setTag(resid);
            } else if (!isNight && resid == R.drawable.price_drop_bg_night){
                resid = R.drawable.price_drop_bg;
                view.setBackgroundResource(resid);
                view.setTag(resid);
            }else if (!isNight && resid == R.drawable.price_rise_bg_night){
                resid = R.drawable.price_rise_bg;
                view.setBackgroundResource(resid);
                view.setTag(resid);
            }
        }
    }

}
